#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::cin;
using std::istream;
using std::ostream;
using std::vector;
using std::string;

class Point;
class Complex
{
public:
    Complex(double real, double imagin);
    Complex(const Point& rhs);
    Complex& operator=(const Complex& rhs);
    Complex& operator=(const Point& rhs);
    void print(void) const;
private:
    double _real;
    double _imagin;
};

class Point
{
friend class Complex;

public:
    Point(double x, double y);
    operator Complex();
private:
    double _x;
    double _y;
};

Point::Point(double x = 0, double y = 0)
    : _x(x)
    , _y(y){
    cout << "Point(double x = 0, double y = 0)" << endl;
}
Complex::Complex(double real = 0, double imagin = 0)
    : _real(real)
    , _imagin(imagin){
    cout << "Complex(double real = 0, double imagin = 0)" << endl;
}

// 构造函数
Complex::Complex(const Point& rhs)
    : _real(rhs._x)
    , _imagin(rhs._y){
    cout << "Complex(const Point& rhs)" << endl;
}

Complex& Complex::operator=(const Complex& rhs){
    _real = rhs._real;
    _imagin = rhs._imagin;
    return *this;
}
Complex& Complex::operator=(const Point& rhs){
    _real = rhs._x;
    _imagin = rhs._y;
    return *this;
}

// 类型转换函数
Point::operator Complex(){
    cout << "operator Complex()" << endl;
    return Complex(_x, _y);
}

void Complex::print(void) const{
    cout << _real << " + ";
    cout << _imagin << "i";
    cout << endl;
}

// 对于如下的赋值操作，请尝试用三种方式实现，并体会各种方式的意义
int main(void)
{
    Point pt(8, 9);
    Complex cx(4, 6);
    // 改造赋值运算符函数
    cx.operator=(pt);
    // 类型转换函数
    cx.operator=(pt.operator Complex());
    // 丑陋的转换
    cx.operator=(Complex(pt));

    cx.print();
    return 0;
}